package lcs.table;

import java.util.LinkedList;
import java.util.List;


/**
 * TableSearcher is a utility class that uses a populated table 
 * to find a single LCS value of a FinderTable.
 */
public class SingleTableSearchHandler {
	//searchResult holds the LCS
	private String searchResult;

	/**
	 * Launches the recursive searchTable, returns solutions
	 * 
	 * @param table The table to search
	 * @return The LCS values of the table.
	 */
	public String searchTable(FinderTable table)
	{
		searchTable(table,table.getStringX().length()-1,table.getStringY().length()-1,"");
		return searchResult;
	}

	/**
	 * Recursively searches the FinderTable for common substructures
	 * 
	 * @param table The table to search.
	 * @param searchX The starting X value of the search.
	 * @param searchY The starting Y value of the search.
	 * @param currResult The lcs as it has been constructed thus far.
	 */
	private String searchTable(FinderTable table, int searchX, int searchY, String currResult) //TODO: clean up code
	{
		boolean complete=false;
		while (!complete){
			if (searchX==0 && searchY==0) { //if the search is over
				if ( //if the first letter of both strings is the same, append
						table.getStringX().charAt(0) ==
						table.getStringY().charAt(0))
					currResult=table.getStringX().charAt(0)+currResult; //append found character to beginning of result string
				searchResult=currResult;
				complete=true;
			} else if ( //if we are searching in the top row, and left is less than current
					searchX==0 &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				currResult=table.getStringX().charAt(searchX)+currResult; //add found match
				searchX=0;searchY=searchY-1; //continue search from 0,y-1
			} else if ( //if we are searching in the leftmost column, and above is less than current
					searchY==0 &&
					table.getElementAt(searchX-1, searchY).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				currResult=table.getStringX().charAt(searchX)+currResult; //add found match
				searchX=searchX-1;searchY=0; //continue search from x-1,0
			} else if ( //if we are searching in the top row, and left is equal to current
					searchX==0 &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchX=0;searchY=searchY-1; //continue search from 0,y-1
			} else if ( //if we are searching in the leftmost column, and above is equal to current
					searchY==0 &&
					table.getElementAt(searchX-1, searchY).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchX=searchX-1;searchY=0; //continue search from x-1,0
			} else if ( //if the element to the left, above, and above and to the left are all less than current
					table.getElementAt(searchX-1, searchY).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX-1, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				currResult=table.getStringX().charAt(searchX)+currResult; //add found match
				searchX=searchX-1;searchY=searchY-1; //continue search from x-1,y-1
			} else if ( //if the element to the left, above left, and above are equal to current
					table.getElementAt(searchX-1, searchY).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX-1, searchY-1).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchX=searchX-1;searchY=searchY-1; //continue search from x-1,y-1
			} else if ( //if the element to the left, and above are equal to current
					table.getElementAt(searchX-1, searchY).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX-1, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchX=searchX-1;//continue search from x-1,y
			} else if ( //if the element to the left is equal to current
					table.getElementAt(searchX-1, searchY).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX-1, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchX=searchX-1; //continue search from x-1,y
			} else if ( //if the element above is equal to current
					table.getElementAt(searchX-1, searchY).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX, searchY-1).getSubstructureLength() == 
					table.getElementAt(searchX, searchY).getSubstructureLength() &&
					table.getElementAt(searchX-1, searchY-1).getSubstructureLength() < 
					table.getElementAt(searchX, searchY).getSubstructureLength()) {
				searchY=searchY-1; //continue search from x,y-1
			}
		}
		return currResult;
	}
}
